"""
配置管理模块
"""
import os
from typing import List, Optional
from pydantic import Field
from pydantic_settings import BaseSettings
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()


class Settings(BaseSettings):
    """应用配置类"""
    
    # Redis配置
    redis_host: str = Field(default="localhost", env="REDIS_HOST")
    redis_port: int = Field(default=6379, env="REDIS_PORT")
    redis_password: Optional[str] = Field(default=None, env="REDIS_PASSWORD")
    redis_db: int = Field(default=0, env="REDIS_DB")
    
    # 数据库配置
    db_host: str = Field(default="localhost", env="DB_HOST")
    db_port: int = Field(default=3306, env="DB_PORT")
    db_user: str = Field(default="root", env="DB_USER")
    db_password: str = Field(default="123456", env="DB_PASSWORD")
    db_name: str = Field(default="user_center", env="DB_NAME")
    
    # 消息队列配置
    audit_queue_name: str = Field(default="content_audit_queue", env="AUDIT_QUEUE_NAME")
    result_queue_name: str = Field(default="audit_result_queue", env="RESULT_QUEUE_NAME")
    
    # 算法配置
    sensitive_words_file: str = Field(default="data/sensitive_words.txt", env="SENSITIVE_WORDS_FILE")
    model_path: str = Field(default="models/", env="MODEL_PATH")
    confidence_threshold: float = Field(default=0.8, env="CONFIDENCE_THRESHOLD")
    
    # 日志配置
    log_level: str = Field(default="INFO", env="LOG_LEVEL")
    log_file: str = Field(default="logs/audit_service.log", env="LOG_FILE")
    
    # API配置
    api_host: str = Field(default="0.0.0.0", env="API_HOST")
    api_port: int = Field(default=8001, env="API_PORT")
    api_workers: int = Field(default=4, env="API_WORKERS")
    
    # 监控配置
    metrics_port: int = Field(default=8002, env="METRICS_PORT")
    enable_metrics: bool = Field(default=True, env="ENABLE_METRICS")
    
    @property
    def redis_url(self) -> str:
        """Redis连接URL"""
        if self.redis_password:
            return f"redis://:{self.redis_password}@{self.redis_host}:{self.redis_port}/{self.redis_db}"
        return f"redis://{self.redis_host}:{self.redis_port}/{self.redis_db}"
    
    @property
    def database_url(self) -> str:
        """数据库连接URL"""
        return f"mysql+pymysql://{self.db_user}:{self.db_password}@{self.db_host}:{self.db_port}/{self.db_name}"
    
    class Config:
        env_file = ".env"
        case_sensitive = False
        protected_namespaces = ('settings_',)


# 全局配置实例
settings = Settings()
